package org.budo.mybatis.dao.statement.builder.creator;

import java.util.Map;

import org.apache.ibatis.session.Configuration;
import org.budo.mybatis.dao.statement.builder.impl.DefaultStatementBuilder;
import org.budo.support.lang.util.StringUtil;

/**
 * @author lmw
 */
public class SaveBatchStatementCreator extends AbstractSaveStatementCreator {
    public SaveBatchStatementCreator(Configuration configuration, DefaultStatementBuilder statementBuilder) {
        super(configuration, statementBuilder);
    }

    protected String getStatementId() {
        return STATEMENT_SAVE_BATCH;
    }

    protected String createStatement(Class<?> entityType) {
        String tableName = this.getStatementBuilder().getEntityConfiger().getTableName(entityType);
        String idFieldName = this.getStatementBuilder().getEntityConfiger().getIdFieldName(entityType);

        Map<String, String> columnAndValues = this.insertColumnAndValues(this.getStatementBuilder().getEntityConfiger(), entityType, false, "item.");

        String statementXml = _4 + "<insert id=\"" + this.getStatementId() + "\" parameterType=\"java.util.List\" useGeneratedKeys=\"true\" keyProperty=\"" + idFieldName + "\">\n" //
                + _8 + " INSERT INTO " + tableName + "\n" //
                + _12 + "(" + StringUtil.join(columnAndValues.keySet(), ",") + ")\n" //
                + _8 + " VALUES \n" //
                + _8 + "<foreach collection=\"list\" item=\"item\" index=\"index\" separator=\",\" >\n"//
                + _12 + " (" + StringUtil.join(columnAndValues.values(), ",") + ")" + "\n" //
                + _8 + "</foreach> " + "\n" //
                + _4 + "</insert>"; //

        return statementXml;
    }
}